package com.jerry.huihome.module.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseViewHolder;
import com.jerry.huihome.base.MyBaseAdapter;
import com.jerry.huihome.module.find.activity.ForemanDetailActivity;
import com.jerry.huihome.module.home.entity.Live;
import com.jerry.huihome.utils.TextParser;
import com.jerry.huihome.views.ExpandGridView;
import com.jerry.huihome.views.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;

import static com.jerry.huihome.module.home.entity.Live.BodyBean.DynaItemsBean.AppraisesBean;

/**
 * Created by LGuipeng on 2016/5/12.
 */
public class PullLiveLoreInfoAdapter extends MyBaseAdapter{
    private boolean isAttention = false;
    private final Context context;
    private final ImageLoader imageLoader;
    private final DisplayImageOptions options;

    public PullLiveLoreInfoAdapter(Context context, List dynaItemsBeenList){
        super(context, dynaItemsBeenList);
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).showImageOnLoading(R.drawable.aliwx_reply_bar_album_nor).showImageOnFail(R.drawable.aliwx_reply_bar_album_press).showImageForEmptyUri(R.drawable.avator_default).build();
        this.context = context;
    }

    @Override
    public int getLayoutId(){
        return R.layout.fragment_live_lore;
    }

    @Override
    public BaseViewHolder getViewHolder(){
        return new ViewHolder();
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder){
        Live.BodyBean.DynaItemsBean dynaItemsBean = (Live.BodyBean.DynaItemsBean) getItem(viewHolder.position);
        final ViewHolder holder = (ViewHolder) viewHolder;
        final int uid = dynaItemsBean.getUid();
        imageLoader.displayImage(dynaItemsBean.getAvatar(), holder.roundImageView, options);
        holder.tvLiveProName.setText("#" + dynaItemsBean.getProjname() + "#");
        holder.tvLivePhase.setText(dynaItemsBean.getPhase());
        holder.tvLiveUsername.setText(dynaItemsBean.getUsername());
        holder.tvLiveContent.setText(dynaItemsBean.getMsgtxt());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = format.format(dynaItemsBean.getOccurTime());
        holder.tvLiveTime.setText(time);
        ExpandGridView gridView = null;
        if(dynaItemsBean.getDynaImages().size() == 1){
            holder.ll_gridView.removeAllViews();
            holder.ll_imageView.setVisibility(View.VISIBLE);
            for(Live.BodyBean.DynaItemsBean.DynaImagesBean imageBean : dynaItemsBean.getDynaImages()){
                imageLoader.displayImage(imageBean.getDetailImg(), holder.imgLiveShow, options);
            }
        }else if(dynaItemsBean.getDynaImages().size() > 1){
            holder.ll_imageView.setVisibility(View.GONE);
            holder.ll_gridView.removeAllViews();
            gridView = new ExpandGridView(context);
            gridView.setColumnWidth((int) (20 * context.getResources().getDisplayMetrics().density + 0.5f));
            gridView.setNumColumns(3);
            gridView.setMinimumHeight(100);
            gridView.setMinimumWidth(100);
            GirlViewImgAdapter imgAdapter = new GirlViewImgAdapter(context, dynaItemsBean.getDynaImages());
            gridView.setAdapter(imgAdapter);
            holder.ll_gridView.addView(gridView);
        }else{
            holder.ll_imageView.setVisibility(View.GONE);
            holder.ll_gridView.removeAllViews();
        }
        int count = dynaItemsBean.getAppraiseCount();
        if(count != 0){
            holder.liveTvAppraise.setVisibility(View.VISIBLE);
            holder.liveTvAppraise.clearComposingText();
            String data = "";
            for(AppraisesBean appraise : dynaItemsBean.getAppraises()){
                data += appraise.getUsername() + ";";
            }
            holder.liveTvAppraise.setText(data);
            //            tvLike.setText(data);
            //            holder.llLiveParams.addView(tvLike);
        }else{
            holder.liveTvAppraise.setVisibility(View.GONE);
        }
        TextView tvContent = null;
        if(dynaItemsBean.getCommentCount() == 0){
            holder.llLive.setVisibility(View.GONE);
        }else{
            holder.llLive.removeAllViews();
            holder.llLive.setVisibility(View.VISIBLE);
            for(Live.BodyBean.DynaItemsBean.CommentsBean content : dynaItemsBean.getComments()){
                tvContent = new TextView(context);
                tvContent.setLayoutParams(new ViewGroup.LayoutParams
                        (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                TextParser textParser = new TextParser();
                if(content.getToUsername() == null){
                    textParser.append(content.getFromUsername(), 56, Color.parseColor("#18BEBB"));
                    textParser.append(":" + content.getMsgtxt() + "", 56, R.color.color_hui);
                }else{
                    textParser.append(content.getFromUsername(), 56, Color.parseColor("#18BEBB"));
                    textParser.append("回复", 56, R.color.color_hui);
                    textParser.append(content.getToUsername() + "", 56, Color.parseColor("#18BEBB"));
                    textParser.append(":" + content.getMsgtxt() + "", 56, R.color.color_hui);
                }
                textParser.parse(tvContent);
                holder.llLive.addView(tvContent);
            }
        }
        holder.tvLiveAttention.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
               TextView tvLiveAttention = (TextView) v;
                if(isAttention){
                    Toast.makeText(context,"已取消关注", Toast.LENGTH_SHORT).show();
                    tvLiveAttention.setTextColor(context.getResources().getColor(R.color.color_light_green));
                    tvLiveAttention.setText("关注");
                    isAttention = false;
                }else{
                    Toast.makeText(context,"已关注", Toast.LENGTH_SHORT).show();
                    tvLiveAttention.setTextColor(context.getResources().getColor(R.color.color_hui));
                    tvLiveAttention.setText("已关注");
                    isAttention = true;
                }
            }
        });
        holder.imgClickPopup.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                View view = LayoutInflater.from(context).inflate(R.layout.layout_popupwindow, null);
                PopupWindow popupWindow = new PopupWindow(view, 600,ViewGroup.LayoutParams.WRAP_CONTENT,true);
                int[] location = new int[2];
                v.getLocationOnScreen(location);
                // 点击空白区域和默认返回按钮取消弹出Window
                // 一般设置背景取消弹出Window
                ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
                popupWindow.setBackgroundDrawable(cd);
                popupWindow.setTouchable(true);
                popupWindow.setOutsideTouchable(true);
                //显示在左边
                popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,
                        location[0]-popupWindow.getWidth(), location[1]-15);
            }
        });
        holder.roundImageView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(context, ForemanDetailActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("pmid",uid);
                context.startActivity(intent);
            }
        });
    }


    public static class ViewHolder extends BaseViewHolder{
        @BindView(R.id.tv_proName)
        TextView tvLiveProName;
        @BindView(R.id.tv_phase)
        TextView tvLivePhase;
        @BindView(R.id.tv_userName)
        TextView tvLiveUsername;
        @BindView(R.id.tv_identity)
        TextView tvLiveIdentity;
        //回复点击
        @BindView(R.id.img_click_popup)
        ImageView imgClickPopup;
        //关注点击
        @BindView(R.id.tv_live_content)
        TextView tvLiveContent;
        @BindView(R.id.tv_live_time)
        TextView tvLiveTime;
        @BindView(R.id.img_live_avatar)
        RoundImageView roundImageView;
        //图片展示
        @BindView(R.id.ll_gridView)
        LinearLayout ll_gridView;
        @BindView(R.id.ll_imageView)
        LinearLayout ll_imageView;
        @BindView(R.id.img_live_show)
        ImageView imgLiveShow;
        @BindView(R.id.tv_live_attention)
        TextView tvLiveAttention;
        //动态添加评论
        @BindView(R.id.live_tv_appraise)
        TextView liveTvAppraise;
        @BindView(R.id.ll_live)
        LinearLayout llLive;
    }
}
